package com.example.controller;

import com.example.dao.LabDAO;
import com.example.dao.LabReservationDAO;
import com.example.model.Lab;
import com.example.model.LabReservation;
import com.example.model.ManagementUser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/labReservations")
public class LabReservationServlet extends HttpServlet {
    private LabReservationDAO labReservationDAO = new LabReservationDAO();
    private LabDAO labDAO = new LabDAO();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String action = request.getParameter("action");
        if (action == null) {
            action = "";
        }

        // 获取当前登录用户
        HttpSession session = request.getSession(false);
        ManagementUser currentUser = (ManagementUser) session.getAttribute("managementUser");

        switch (action) {
            case "add":
                addReservation(request, response, currentUser);
                break;
            case "search":
                searchReservations(request, response);
                break;
            case "adminSearch":
                adminSearchReservations(request, response);
                break;
            case "viewAll":
                viewAllReservations(request, response);
                break;
            case "listAllLabs":
                listAllLabs(request, response);
                break;
            default:
                viewMyReservations(request, response, currentUser);
                break;
        }
    }

    // 显示所有实验室
    private void listAllLabs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Lab> labs = labDAO.findAllLabs();
        request.setAttribute("labs", labs);
        request.getRequestDispatcher("/jsp/labReservation/listAllLabs.jsp").forward(request, response);
    }

    // 显示我的预约
    private void viewMyReservations(HttpServletRequest request, HttpServletResponse response, ManagementUser currentUser) throws ServletException, IOException {
        if (currentUser != null) {
            List<LabReservation> reservations = labReservationDAO.findAllReservationsByUserId(currentUser.getManagementId());
            request.setAttribute("reservations", reservations);
            request.getRequestDispatcher("/jsp/labReservation/myReservations.jsp").forward(request, response);
        } else {
            response.sendRedirect("login");
        }
    }

    // 显示所有预约（管理员）
    private void viewAllReservations(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<LabReservation> reservations = labReservationDAO.findAllReservations();
        request.setAttribute("reservations", reservations);
        request.getRequestDispatcher("/jsp/labReservation/allReservations.jsp").forward(request, response);
    }

    // 添加预约
    private void addReservation(HttpServletRequest request, HttpServletResponse response, ManagementUser currentUser) throws ServletException, IOException {
        if (currentUser == null) {
            response.sendRedirect("login");
            return;
        }

        long labId = Long.parseLong(request.getParameter("labId"));
        String reservationDate = request.getParameter("reservationDate");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String purpose = request.getParameter("purpose");

        LabReservation reservation = new LabReservation();
        reservation.setLabId(labId);
        reservation.setUserId(currentUser.getManagementId());
        reservation.setReservationDate(reservationDate);
        reservation.setStartTime(startTime);
        reservation.setEndTime(endTime);
        reservation.setPurpose(purpose);

        boolean success = labReservationDAO.addReservation(reservation);
        if (success) {
            // 预约成功，跳转到我的预约页面
            response.sendRedirect("labReservations");
        } else {
            request.setAttribute("error", "预约失败");
            request.setAttribute("labId", labId);
            request.getRequestDispatcher("/jsp/labReservation/reserve.jsp").forward(request, response);
        }
    }

    // 搜索
    private void searchReservations(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        List<LabReservation> reservations = labReservationDAO.searchReservations(keyword);
        request.setAttribute("reservations", reservations);
        request.setAttribute("keyword", keyword);
        request.getRequestDispatcher("/jsp/labReservation/myReservations.jsp").forward(request, response);
    }

    // 管理员搜索
    private void adminSearchReservations(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        List<LabReservation> reservations = labReservationDAO.searchReservations(keyword);
        request.setAttribute("reservations", reservations);
        request.setAttribute("keyword", keyword);
        request.getRequestDispatcher("/jsp/labReservation/allReservations.jsp").forward(request, response);
    }
}